﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using Home.Mobilesales.Common;
using Home.MobileSales.Common.Entities;


namespace Home.MobileSales.Common.Models
{
    public class TradeMarkRepository
    {
        public TradeMark GetTradeMarkById(int tradeMarId)
        {
            using (SqlCeConnection connection = WorkDataBase.CreateConnection())
            {
                if (connection.State != ConnectionState.Open)
                    connection.Open();

                string sqlQuery = "Select IdTradeMark, NameTradeMark from TradeMarks where IdTradeMark = @IdTradeMark";
                using (SqlCeCommand command = new SqlCeCommand(sqlQuery, connection))
                {
                    command.Parameters.Add("@IdTradeMark", tradeMarId);
                    using (SqlCeDataReader dr = command.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            int idTradeMark = dr.GetOrdinal("IdTradeMark");
                            int nameTradeMark = dr.GetOrdinal("NameTradeMark");
                            TradeMark tradeMark = new TradeMark();
                            tradeMark.IdTradeMark = dr.GetInt32(idTradeMark);
                            tradeMark.Name = dr.GetString(nameTradeMark);
                            return tradeMark;
                        }
                        return null;
                    }
                }
            }
        }
    }
}
